/**
 * Project Name:mauto-web
 * File Name:ApiEnterpriseController.java
 * Package Name:com.inftech.mauto.sys.web.front
 * Date:2016年10月17日下午9:51:38
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.core.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.BusinessConstant;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.core.entity.Comment;
import com.inftech.mauto.core.entity.Follow;
import com.inftech.mauto.core.entity.Order;
import com.inftech.mauto.core.service.CommentService;
import com.inftech.mauto.core.service.FollowService;
import com.inftech.mauto.core.service.OrderService;
import com.inftech.mauto.sys.service.DictService;
import com.inftech.mauto.sys.service.EnterpriseService;
import com.mingsoft.basic.constant.e.SessionConstEnum;
import com.mingsoft.basic.entity.ManagerSessionEntity;
import com.mingsoft.basic.interceptor.BaseInterceptor;
import com.mingsoft.util.StringUtil;

import net.mingsoft.basic.util.BasicUtil;

/**
 * ClassName:ApiOrderController <br/>
 * Function: 订单接口. <br/>
 * Date: 2016年10月17日 下午9:51:38 <br/>
 * 
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/${managerPath}/order")
public class OrderController extends BaseController {

	/**
	 * 注入订单业务层
	 */
	@Autowired
	protected OrderService orderService;

	/**
	 * 注入服务单位业务层
	 */
	@Autowired
	protected EnterpriseService enterpriseService;

	/**
	 * 注入回访业务层
	 */
	@Autowired
	protected FollowService followService;

	/**
	 * 注入评价业务层
	 */
	@Autowired
	protected CommentService commentService;

	/**
	 * 注入数据字典业务层
	 */
	@Autowired
	protected DictService dictService;

	/**
	 * 
	 * list:获取订单列表. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request) throws Exception {
		try {
			BasicUtil.startPage();
			Map<String, Object> whereMap = BasicUtil.assemblyRequestMap();
			Object orderCreateTime = whereMap.get("orderCreateTime");
			whereMap.put("orderCreateTime", null);
			if (!StringUtil.isBlank(orderCreateTime)) {
				whereMap.put("orderCreateTimeBegin", orderCreateTime.toString().split("至")[0]);
				whereMap.put("orderCreateTimeEnd", orderCreateTime.toString().split("至")[1]);
			}
			Object orderAppointTime = whereMap.get("orderAppointTime");
			whereMap.put("orderAppointTime", null);
			if (!StringUtil.isBlank(orderAppointTime)) {
				whereMap.put("orderAppointTimeBegin", orderAppointTime.toString().split("至")[0]);
				whereMap.put("orderAppointTimeEnd", orderAppointTime.toString().split("至")[1]);
			}
			// 获取订单列表
			List<Order> orderList = orderService.queryOrderList(whereMap);
			BasicUtil.endPage(orderList);
			request.setAttribute("orderList", orderList);
			request.setAttribute("pageNo",
					CommonUtils.isBlank(request.getParameter("pageNo")) ? 1 : request.getParameter("pageNo"));
			LOG.info("后台获取订单列表成功");
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("后台获取订单列表失败", e);
		}
		return view("/mauto/core/order_list");
	}

	/**
	 * 
	 * detail:获取订单详情. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/{orderCode}/detail", produces = "text/html;charset=UTF-8")
	public String detail(@PathVariable String orderCode, HttpServletRequest request) throws Exception {
		try {
			// 获取用户对象
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("order_code", orderCode);
			// 订单信息
			Order order = orderService.queryOrder(paramMap);
			request.setAttribute("order", order);
			// 服务单位信息
			// Map<String, Object> enterprise =
			// enterpriseService.queryEnterprise(order.getEnterpriseId());
			// request.setAttribute("enterprise", enterprise);
			if (order.getOrderCommentStatus().equals(BusinessConstant.OCS_YPJ)) {
				// 评价信息
				Comment comment = commentService.queryComment(order.getId(), BusinessConstant.COMMENT_TYPE_BP);
				request.setAttribute("comment", comment);
			}
			// 回访信息
			if (order.getOrderFollowStatus().equals(BusinessConstant.OFS_YHF)) {
				Follow follow = followService.queryFollow(order.getId());
				request.setAttribute("follow", follow);
			}

			// 数据字典-取消原因
			request.setAttribute("cancelReasonList", dictService.queryDictList(BusinessConstant.CANCEL_REASON_KEY));
			// 传递参数
			request.setAttribute(BaseInterceptor.PARAMS, BasicUtil.assemblyRequestUrlParams(null));
			// 当前时间
			request.setAttribute("now", CommonUtils.getNowD());
			LOG.info("后台获取订单详情成功");
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("后台获取订单详情失败", e);
		}
		return view("/mauto/core/order_detail");
	}

	/**
	 * 
	 * cancel:取消订单. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/cancel", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String cancel(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
					SessionConstEnum.MANAGER_SESSION);
			Map<String, Object> paramMap = BasicUtil.assemblyRequestMap();
			orderService.modifyOrderCancel(paramMap, managerSession, false);
			LOG.info("后台取消订单成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("后台取消订单失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * check:审核订单. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/check", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String check(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
					SessionConstEnum.MANAGER_SESSION);
			Map<String, Object> paramMap = BasicUtil.assemblyRequestMap();
			paramMap.put("managerId", managerSession.getManagerId());
			orderService.modifyOrderCheck(paramMap);
			LOG.info("后台审核订单成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("后台审核订单失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * follow:回访订单. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/follow", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String follow(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
					SessionConstEnum.MANAGER_SESSION);
			Map<String, Object> paramMap = BasicUtil.assemblyRequestMap();
			paramMap.put("managerId", managerSession.getManagerId());
			orderService.modifyOrderFollow(paramMap);
			LOG.info("后台回访提交成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("后台回访提交失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

}
